package com.imaker.frame.network

import android.util.Log
import com.imaker.frame.utils.UserInfoManager
import okhttp3.Interceptor
import okhttp3.Interceptor.Chain
import okhttp3.Request.Builder
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * @author ziheng
 * @Description:
 * @date 2022/12/19 16:39
 */
class HeaderInterceptor : Interceptor {

    private val TAG = "HeaderInterceptor"
    @Throws(IOException::class)
    override fun intercept(chain: Chain): Response {
        val original: Request = chain.request()
        val requestBuilder: Builder = original.newBuilder()
            .addHeader("MicoMico-access", "DVQBb87weBxKzJleonfsE0/J5CekKMbX3bs+/HXlQgc=")
        if(UserInfoManager.instance.getUserLoginState()){
            Log.i(TAG, "intercept: ${UserInfoManager.instance.getUserToekn()}")
            requestBuilder.addHeader("token", UserInfoManager.instance.getUserToekn()!!)
        }
        val request: Request = requestBuilder.build()
        return chain.proceed(request)
    }
}